package classes;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

import containers.KeywordContainer;
import containers.StartContainer;
import containers.StatisticsContainer;

public class UserInterface {
	
	public UserInterface() {

	}
	
	public StartContainer startScript() {
		Scanner scan = new Scanner(System.in);
		List<String> keywords = new ArrayList<String>();
		String keywordList;
		String URL = "";
		boolean ok = false;
		URL seed = null;
		
		System.out.println("Welcome to the Single-Threaded Webcrawler.");
				
		//get keywords
		while(!ok) {
			System.out.println("Please enter a list of keywords to search for, up to ten, seperated by spaces: ");
			scan = new Scanner(System.in);
			
			//get keywords
			keywordList = scan.nextLine();
			StringTokenizer st = new StringTokenizer(keywordList);
						
			while(st.hasMoreTokens()) {
				keywords.add(st.nextToken(" "));
			}
			
			//make sure # of keywords are greater than 0 but less than 11.
			if (keywords.size() > 0 && keywords.size() < 11) {
				ok = true;
			}
		}
		
		//set up for next input
		ok = false;
		
		//set seed url
		while(!ok) {
			scan = new Scanner(System.in);
			System.out.println("Please enter the starting url: ");
			URL = scan.nextLine();
			
			try {
				seed = new URL(URL);
				if(!URL.isEmpty()) {
					ok = true;
				}
			} catch (MalformedURLException e) {
				System.out.println("Given URL is not valid.  Please try again.");
			}
		}
		
		return new StartContainer(keywords, seed);
	}
	
	public void endScript(final StatisticsContainer mySC) {
		StringBuilder sb = new StringBuilder();
		
		sb.append("\n\n");
		
		sb.append("Pages Retrieved:  ");
		sb.append(mySC.getNumberTotalPagesCrawled());
		sb.append("\n");
		
		sb.append("Average words per page: ");
		sb.append(mySC.getMyAverageWordsPerPage());
		sb.append("\n");
		
		sb.append("Average URLs per page: ");
		sb.append(mySC.getMyAverageNumURLPerPage());
		sb.append("\n");
		
		sb.append(String.format("%1$10s  %2$5s  %3$10s  %4$5s %5$10s\n", "Keyword", " ", 
				"Avg. hits per page", " ", "Total hits"));
		
		for(KeywordContainer ksc: mySC.getKeywords()) {
			sb.append(String.format("%1$10s  %2$5s %3$10s  %4$10s %5$10s\n", ksc.getMyKeyword(),
					" ", roundThreeDecimals(ksc.getAverageOccurences()), " ", ksc.getTotalOccurences()));
		}
		
		sb.append("Average parse time per pages; ");
		sb.append(mySC.getMyAverageTimePerPage());
		sb.append(" sec");
		sb.append("\n");
		
		sb.append("Total running time: ");
		sb.append(mySC.getMyTotalExecutionTime());
		sb.append(" sec");
		
		System.out.println(sb.toString());
		System.out.println("Done");
	}
	
	private double roundThreeDecimals(final double theNumber) {
		DecimalFormat numFormat = new DecimalFormat("0.000");
		return Double.valueOf(numFormat.format(theNumber));
	}
}
